package zjh_20230406;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class Demo02Recive {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket accept = serverSocket.accept();

            System.out.println("有数据连接" + accept.getInetAddress().getHostName());

            int length = 0;
            byte[] bytes = new byte[1024];

            InputStream inputStream = accept.getInputStream();

            FileOutputStream fileOutputStream = new FileOutputStream("D:\\kaifamiao\\new.jpg");

            while ((length = inputStream.read(bytes)) != -1 ){
                fileOutputStream.write(bytes,0,length);
            }

            fileOutputStream.close();

            System.out.println("上传成功");
            accept.getOutputStream().write("上传成功".getBytes());
            accept.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
